package com.java.Controller;

import com.java.Service.AdminService;
import com.java.bean.Message;
import com.java.mvc.ResponseBody;
import com.java.util.JSONUtil;
import org.apache.http.HttpRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Author Clarklevis
 * @Date 2021/5/24
 */
public class AdminController {

    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        //1 接参数
        String username = request.getParameter("username");
        //2 调用servlet传参数，并获取结果
        String password = request.getParameter("password");
        //3 根据结果，准备不同的数据给Ajax
        boolean result = AdminService.login(username, password);
        Message msg = null;
        if (result){
            msg = new Message("登陆成功", 0);
            //登陆时间和ip更新(获得客户端的登录ip地址)
            Date date = new Date();
            String ip = request.getRemoteAddr();
            AdminService.updateLoginTimeAndIp(username, date, ip);
        }else {
            msg = new Message("登录失败", -1);
        }
        //4 将数据转化为json并返回
        String json = JSONUtil.toJSON(msg);
        return json;

    }


}
